package icasue.reflect.handles.object;
import lombok.SneakyThrows;
import icasue.reflect.handles.OAble;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Objects;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/11/30 下午5:47
 * @UpdateDate:
 * @Description:
 */
public class ObjectO implements OAble {

    static {
        try {
            Class<?> loadHandleOperate = ObjectO.class.getClassLoader().loadClass("icasue.reflect.handles.HandleSupplier");
            ObjectO.lookup = (MethodHandles.Lookup)loadHandleOperate.getDeclaredField("methodLookup").get(null);
        }catch (Throwable e){
            throw new RuntimeException("ObjectO init error.",e);
        }
    }

    static {
        findHandles();
    }

    public static MethodHandles.Lookup lookup;

    //从类中获取方法 所有权限
    public static MethodHandle isNull;
    public static MethodHandle notNull;
    public static MethodHandle toString;
    public static MethodHandle getNull;
    public static MethodHandle equals;


    @SneakyThrows(Throwable.class)
    private static void findHandles(){
        isNull = lookup.findStatic(Objects.class, "isNull", MethodType.methodType(boolean.class, Object.class));
        notNull = lookup.findStatic(Objects.class, "nonNull", MethodType.methodType(boolean.class, Object.class));
        toString = lookup.findStatic(Objects.class, "toString", MethodType.methodType(String.class, Object.class));
        getNull = MethodHandles.constant(Object.class,null);
        equals = lookup.findStatic(Objects.class, "equals", MethodType.methodType(boolean.class,Object.class, Object.class));
    }
}
